﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using SHDocVw;

using SBPweb.Modules.Smartparts;
using SBPweb.Modules.Modularity;
using SBPweb.Modules.Communication;

namespace SBPweb.MediaCenter.Modules.Browser.WF
{
	//TODO: Save url's to registry
	//TODO: Save visibility and location for toolstrips
    public partial class BrowserControl : UserControl, ISmartpart
    {
		private bool mPromptUserBeforePrint = true;

		#region Private fields

		private bool pIsModal = false;
		private Aligning pAlign = Aligning.Fill;
		private IModule pParentModule = null;

		private Guid cmdGuid = new Guid("ED016940-BD5B-11CF-BA4E-00C04FD70816");
		private AxSHDocVw.AxWebBrowser _ActiveWebBrowser;

		private bool IsPrinterEnabled
		{
			get
			{
				int response = (int)_ActiveWebBrowser.QueryStatusWB(SHDocVw.OLECMDID.OLECMDID_PRINT);
				return (response & (int)SHDocVw.OLECMDF.OLECMDF_ENABLED) != 0 ? true : false;
			}
		}

		#endregion

		#region Constructor logic

		public BrowserControl(IModule parentModule)
			: this(parentModule, new Uri("about:blank", UriKind.RelativeOrAbsolute))
		{
		}

		public BrowserControl(IModule parentModule, Uri uri)
		{
			pParentModule = parentModule;
			InitializeComponent();
			InitializeToolStrips();
			InitializeUrlList();
			this.Dock = System.Windows.Forms.DockStyle.Fill;
			axWebBrowser.Navigate(uri.OriginalString);
			tstbUrl.Text = uri.ToString();

			axWebBrowser.Tag = new HE_WebBrowserTag();
			SetActiveWebBrowser();
		}

		private void InitializeToolStrips()
		{
			//TODO: Load visibility and location for toolstrips
			tsNavigate.Location = new System.Drawing.Point(3, 0);
			tsNavigate.Visible = true;
			tsRefresh.Location = new System.Drawing.Point(456, 0);
			tsRefresh.Visible = true;
			tsFind.Location = new System.Drawing.Point(515, 0);
			tsFind.Visible = true;
			tsFile.Location = new System.Drawing.Point(3, 25);
			tsFile.Visible = true;
			tsSelect.Location = new System.Drawing.Point(188, 25);
			tsSelect.Visible = true;
			tsMisc.Location = new System.Drawing.Point(298, 25);
			tsMisc.Visible = true;

			navigationToolStripMenuItem.Checked = tsNavigate.Visible;
			refreshToolStripMenuItem.Checked = tsRefresh.Visible;
			findToolStripMenuItem.Checked = tsFind.Visible;
			fileToolStripMenuItem.Checked = tsFile.Visible;
			selectToolStripMenuItem.Checked = tsSelect.Visible;
			miscToolStripMenuItem.Checked = tsMisc.Visible;
		}

		private void InitializeUrlList()
		{
			//TODO: Load url's from registry			
			tstbUrl.BeginUpdate();
			tstbUrl.Items.Clear();
			tstbUrl.Items.Add("index.hu");
			tstbUrl.Items.Add("ize.hu");
			tstbUrl.Items.Add("standby.blog.hu");
			tstbUrl.Items.Add("subba.blog.hu");
			tstbUrl.Items.Add("szanalmas.hu");
			tstbUrl.Items.Add("www.google.com");
			tstbUrl.EndUpdate();
		}

		#endregion

		#region Property accessors

		public Aligning Align
		{
			get
			{
				return pAlign;
			}
			set
			{
				pAlign = value;
			}
		}

		public IModule ParentModule
		{
			get
			{
				return pParentModule;
			}
		}

		public Module BrowserModule
		{
			get
			{
				if (ParentModule != null && ParentModule is Module)
				{
					return ParentModule as Module;
				}
				return null;
			}
		}

		public bool IsVisible
		{
			get
			{
				return base.Visible;
			}
			set
			{
				base.Visible = value;
			}
		}

		public bool IsEnabled
		{
			get
			{
				return base.Enabled;
			}
			set
			{
				base.Enabled = value;
			}
		}

		public bool IsModal
		{
			get
			{
				return pIsModal;
			}
		}

		#endregion

		#region Private helper methods

		private void SetActiveWebBrowser()
		{
			TabPage _TabPage;
			_TabPage = tcBrowsers.SelectedTab;
			for (int i = 0; i < _TabPage.Controls.Count; i++)
			{
				if (_TabPage.Controls[i].GetType().Name == "AxWebBrowser")
				{
					_ActiveWebBrowser = (AxSHDocVw.AxWebBrowser)_TabPage.Controls[i];
					HE_WebBrowserTag _HE_WebBrowserTag;

					_HE_WebBrowserTag = (HE_WebBrowserTag)_ActiveWebBrowser.Tag;
					_HE_WebBrowserTag._TabIndex = tcBrowsers.SelectedIndex;
					tsslInfo.Text = _ActiveWebBrowser.LocationURL;
					tsbBack.Enabled = _HE_WebBrowserTag._CanBack;
					tsbNext.Enabled = _HE_WebBrowserTag._CanForward;
					tstbUrl.Text = _ActiveWebBrowser.LocationURL;
					break;
				}
			}
		}

		private AxSHDocVw.AxWebBrowser CreateNewWebBrowser()
		{
			AxSHDocVw.AxWebBrowser _axWebBrowser = new AxSHDocVw.AxWebBrowser();
			_axWebBrowser.Tag = new HE_WebBrowserTag();
			TabPage _TabPage = new TabPage();
			_TabPage.Controls.Add(_axWebBrowser);

			_axWebBrowser.Dock = DockStyle.Fill;
			_axWebBrowser.BeforeNavigate2 += new AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(this.axWebBrowser_BeforeNavigate2);
			_axWebBrowser.CommandStateChange += new AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEventHandler(this.axWebBrowser_CommandStateChange);
			_axWebBrowser.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser_DocumentComplete);
			_axWebBrowser.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser_NavigateComplete2);
			_axWebBrowser.NavigateError += new AxSHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(this.axWebBrowser_NavigateError);
			_axWebBrowser.NewWindow2 += new AxSHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(this.axWebBrowser_NewWindow2);
			_axWebBrowser.ProgressChange += new AxSHDocVw.DWebBrowserEvents2_ProgressChangeEventHandler(this.axWebBrowser_ProgressChange);
			_axWebBrowser.StatusTextChange += new AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(this.axWebBrowser_StatusTextChange);
			_axWebBrowser.TitleChange += new AxSHDocVw.DWebBrowserEvents2_TitleChangeEventHandler(this.axWebBrowser_TitleChange);
			_axWebBrowser.WindowClosing +=new AxSHDocVw.DWebBrowserEvents2_WindowClosingEventHandler(this.axWebBrowser_WindowClosing);

			tcBrowsers.TabPages.Add(_TabPage);
			tcBrowsers.SelectedTab = _TabPage;
			tsbCloseTab.Enabled = true;
			return _axWebBrowser;
		}

		private mshtml.HTMLDocument GetDocument()
		{
			try
			{
				mshtml.HTMLDocument htm = (mshtml.HTMLDocument)_ActiveWebBrowser.Document;
				return htm;
			}
			catch
			{
				throw (new Exception("Cannot retrieve the document from the WebBrowser control"));
			}
		}

		private void ExecCommandID(SHDocVw.OLECMDID _OLECMDID)
		{
			int response = (int)_ActiveWebBrowser.QueryStatusWB(_OLECMDID);
			bool IsOK = (response & (int)SHDocVw.OLECMDF.OLECMDF_ENABLED) != 0 ? true : false;
			if (IsOK == false) { return; }

			Object o = null;
			_ActiveWebBrowser.ExecWB(_OLECMDID, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
		}

		#endregion

		#region WebBrowser handler methods (Handling from outside?)

		public void WebBrowserGo()
		{
			tsslInfo.Text = "";
			string url = tstbUrl.Text;

			if (url == "")
			{
				tcBrowsers.SelectedTab.Text = "blank";
				return;
			}
			try
			{
				Cursor.Current = Cursors.WaitCursor;
				Object o = null;
				_ActiveWebBrowser.Navigate(url, ref o, ref o, ref o, ref o);
			}
			finally
			{
				if (tstbUrl.Items.IndexOf(url) == -1)
				{
					tstbUrl.Items.Add(url);
				}

				Cursor.Current = Cursors.Default;
			}
		}

		public void WebBrowserGoBack()
		{
			_ActiveWebBrowser.GoBack();
		}

		public void WebBrowserGoNext()
		{
			_ActiveWebBrowser.GoForward();
		}

		public void WebBrowserGoHome()
		{
			_ActiveWebBrowser.GoHome();
		}

		public void WebBrowserStop()
		{
			_ActiveWebBrowser.Stop();
			tsslInfo.Text = "Stopped";
			tspbProgress.Value = 0;
		}

		public void WebBrowserRefresh()
		{
			object REFRESH_COMPLETELY = 3;
			_ActiveWebBrowser.Refresh2(ref REFRESH_COMPLETELY);
		}

		public void WebBrowserFind()
		{
			IOleCommandTarget cmdt;
			Object o = new object();
			try
			{
				cmdt = (IOleCommandTarget)GetDocument();
				cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Find,
					(uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
			}
			catch (Exception e)
			{
				System.Windows.Forms.MessageBox.Show(e.Message);
			}
		}

		public void WebBrowserSearch()
		{
			_ActiveWebBrowser.GoSearch();
		}

		public void WebBrowserAddTab()
		{
			tsbCloseTab.Enabled = tcBrowsers.TabCount > 1;
			CreateNewWebBrowser();
			tstbUrl.Focus();
			tstbUrl.Text = "about:blank";
			WebBrowserGo();
		}

		public void WebBrowserCloseTab()
		{
			tcBrowsers.SelectedTab.Dispose();
			if (tcBrowsers.TabCount == 0)
			{
				WebBrowserAddTab();
			}
			else
			{
				SetActiveWebBrowser();
			}
			tsbCloseTab.Enabled = tcBrowsers.TabCount > 1;
		}

		public void WebBrowserOpenFile()
		{
			OpenFileDialog _openFileDialog = new OpenFileDialog();
			_openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
			_openFileDialog.FilterIndex = 2;
			_openFileDialog.RestoreDirectory = true;

			if (_openFileDialog.ShowDialog() == DialogResult.OK)
			{
				tstbUrl.Text = _openFileDialog.FileName;
				WebBrowserGo();
			}
		}

		public void WebBrowserSave()
		{
			ExecCommandID(SHDocVw.OLECMDID.OLECMDID_SAVEAS);
		}

		public void WebBrowserPrintPage()
		{
			object o = "";
			SHDocVw.OLECMDID Print = SHDocVw.OLECMDID.OLECMDID_PRINT;
			SHDocVw.OLECMDEXECOPT PromptUser = SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER;
			SHDocVw.OLECMDEXECOPT DontPromptUser = SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER;

			if (!IsPrinterEnabled)
			{
				return;
			}
			if (mPromptUserBeforePrint)
			{
				axWebBrowser.ExecWB(Print, PromptUser, ref o, ref o);
			}
			else
			{
				_ActiveWebBrowser.ExecWB(Print, DontPromptUser, ref o, ref o);
			}
		}

		public void WebBrowserPrintPreview()
		{
			ExecCommandID(SHDocVw.OLECMDID.OLECMDID_PRINTPREVIEW);
		}

		public void WebBrowserPrintPageSetup()
		{
			ExecCommandID(SHDocVw.OLECMDID.OLECMDID_PAGESETUP);
		}

		public void WebBrowserViewSource()
		{
			IOleCommandTarget cmdt;
			Object o = new object();
			try
			{
				cmdt = (IOleCommandTarget)GetDocument();
				cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.ViewSource,
					(uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
			}
			catch (Exception e)
			{
				System.Windows.Forms.MessageBox.Show(e.Message);
			}
		}

		public void WebBrowserCopy()
		{
			ExecCommandID(SHDocVw.OLECMDID.OLECMDID_COPY);
		}

		public void WebBrowserCut()
		{
			ExecCommandID(SHDocVw.OLECMDID.OLECMDID_CUT);
		}

		public void WebBrowserPaste()
		{
			ExecCommandID(SHDocVw.OLECMDID.OLECMDID_PASTE);
		}

		public void WebBrowserSelectAll()
		{
			ExecCommandID(SHDocVw.OLECMDID.OLECMDID_SELECTALL);
		}

		public void WebBrowserPageProperties()
		{
			ExecCommandID(SHDocVw.OLECMDID.OLECMDID_PROPERTIES);
		}

		public void WebBrowserInternetOptions()
		{
			IOleCommandTarget cmdt;
			Object o = new object();
			try
			{
				cmdt = (IOleCommandTarget)GetDocument();
				cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Options,
					(uint)SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref o, ref o);
			}
			catch
			{
				// NOTE: Because of the way that this CMDID is handled in Internet Explorer,
				// this catch block will always fire, even though the dialog box
				// and its operations completed successfully. You can suppress this
				// error without causing any damage to your host.
			}
		}

		#endregion

		#region UI event handlers

		#region ToolStrip buttons

		private void tsbBack_Click(object sender, EventArgs e)
		{
			WebBrowserGoBack();
		}

		private void tsbNext_Click(object sender, EventArgs e)
		{
			WebBrowserGoNext();
		}

		private void tstbUrl_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Return)
			{
				WebBrowserGo();
			}
		}

		private void tsbGo_Click(object sender, EventArgs e)
		{
			WebBrowserGo();
		}

		private void tsbStop_Click(object sender, EventArgs e)
		{
			WebBrowserStop();
		}

		private void tsbRefresh_Click(object sender, EventArgs e)
		{
			WebBrowserRefresh();
		}

		private void tsbFind_Click(object sender, EventArgs e)
		{
			WebBrowserFind();
		}

		private void tsbHome_Click(object sender, EventArgs e)
		{
			WebBrowserGoHome();
		}

		private void tsbSearch_Click(object sender, EventArgs e)
		{
			WebBrowserSearch();
		}

		private void tsbAddTab_Click(object sender, EventArgs e)
		{
			WebBrowserAddTab();
		}

		private void tsbCloseTab_Click(object sender, EventArgs e)
		{
			WebBrowserCloseTab();
		}

		private void tsbOpen_Click(object sender, EventArgs e)
		{
			WebBrowserOpenFile();
		}

		private void tsbSave_Click(object sender, EventArgs e)
		{
			WebBrowserSave();
		}

		private void tsbPrint_Click(object sender, EventArgs e)
		{
			WebBrowserPrintPage();
		}

		private void tsbPrintPreview_Click(object sender, EventArgs e)
		{
			WebBrowserPrintPreview();
		}

		private void tsbPageSetup_Click(object sender, EventArgs e)
		{
			WebBrowserPrintPageSetup();
		}

		private void tsbCopy_Click(object sender, EventArgs e)
		{
			WebBrowserCopy();
		}

		private void tsbCut_Click(object sender, EventArgs e)
		{
			WebBrowserCut();
		}

		private void tsbPaste_Click(object sender, EventArgs e)
		{
			WebBrowserPaste();
		}

		private void tsbSelectAll_Click(object sender, EventArgs e)
		{
			WebBrowserSelectAll();
		}

		private void tsbSource_Click(object sender, EventArgs e)
		{
			WebBrowserViewSource();
		}

		private void tsbProperties_Click(object sender, EventArgs e)
		{
			WebBrowserPageProperties();
		}

		private void tsbOptions_Click(object sender, EventArgs e)
		{
			WebBrowserInternetOptions();
		}

		#endregion

		private void tcBrowsers_SelectedIndexChanged(object sender, EventArgs e)
		{
			SetActiveWebBrowser();
		}

		#region Control events

		private void BrowserControl_Load(object sender, EventArgs e)
		{
			tstbUrl.Focus();
			tstbUrl.Text = "about:blank";
			WebBrowserGo();
		}

		private void BrowserControl_Resize(object sender, EventArgs e)
		{
			tsslInfo.Size = new System.Drawing.Size(this.Width - tspbProgress.Width - 30, tsslInfo.Height);
		}

		#endregion

		#region Context menu

		private void navigationToolStripMenuItem_Click(object sender, EventArgs e)
		{
			tsNavigate.Visible = navigationToolStripMenuItem.Checked;
		}

		private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
		{
			tsRefresh.Visible = refreshToolStripMenuItem.Checked;
		}

		private void findToolStripMenuItem_Click(object sender, EventArgs e)
		{
			tsFind.Visible = findToolStripMenuItem.Checked;
		}

		private void fileToolStripMenuItem_Click(object sender, EventArgs e)
		{
			tsFile.Visible = fileToolStripMenuItem.Checked;
		}

		private void selectToolStripMenuItem_Click(object sender, EventArgs e)
		{
			tsSelect.Visible = selectToolStripMenuItem.Checked;
		}

		private void miscToolStripMenuItem_Click(object sender, EventArgs e)
		{
			tsMisc.Visible = miscToolStripMenuItem.Checked;
		}

		#endregion

		#endregion

		#region axWebBrowser Event handlers

		private void axWebBrowser_ProgressChange(object sender, AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e)
		{
			AxSHDocVw.AxWebBrowser axWebBrowser1 = (AxSHDocVw.AxWebBrowser)sender;
			HE_WebBrowserTag _HE_WebBrowserTag = (HE_WebBrowserTag)axWebBrowser1.Tag;

			//If current tab page does not match current webbrowser then leave it.
			if (_HE_WebBrowserTag._TabIndex != tcBrowsers.SelectedIndex) { return; }

			tspbProgress.Visible = true;
			if ((e.progress > 0) && (e.progressMax > 0))
			{
				tspbProgress.Maximum = e.progressMax;
				tspbProgress.Step = e.progress;
				tspbProgress.PerformStep();
			}
			else if (axWebBrowser1.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
			{
				tspbProgress.Value = 0;
				tspbProgress.Visible = false;
			}

		}

		private void axWebBrowser_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e)
		{
			HE_WebBrowserTag _HE_WebBrowserTag = (HE_WebBrowserTag)_ActiveWebBrowser.Tag;
			if (_HE_WebBrowserTag._TabIndex != tcBrowsers.SelectedIndex) { return; }

			tsbStop.Enabled = true;
			tsslInfo.Text = "";
			tsslInfo.Text = "Loading...";
		}

		private void axWebBrowser_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
		{
			if (_ActiveWebBrowser.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
			{
				HE_WebBrowserTag _HE_WebBrowserTag = (HE_WebBrowserTag)_ActiveWebBrowser.Tag;
				if (_HE_WebBrowserTag._TabIndex == tcBrowsers.SelectedIndex)
				{ tsslInfo.Text = _ActiveWebBrowser.LocationURL; }

				tspbProgress.Value = 0;
				tspbProgress.Visible = false;

				tsbPrint.Enabled = IsPrinterEnabled;
			}
		}

		private void axWebBrowser_StatusTextChange(object sender, AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
		{
			AxSHDocVw.AxWebBrowser _AxWebBrowser = (AxSHDocVw.AxWebBrowser)sender;
			HE_WebBrowserTag _HE_WebBrowserTag = (HE_WebBrowserTag)_AxWebBrowser.Tag;
			if (_HE_WebBrowserTag._TabIndex != tcBrowsers.SelectedIndex)
			{
				tsslInfo.Text = "";
				return;
			}
			else
			{
				tsslInfo.Text = e.text;
			}

		}

		private void axWebBrowser_NavigateError(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateErrorEvent e)
		{
			HE_WebBrowserTag _HE_WebBrowserTag = (HE_WebBrowserTag)_ActiveWebBrowser.Tag;
			if (_HE_WebBrowserTag._TabIndex != tcBrowsers.SelectedIndex) { return; }

			Cursor.Current = Cursors.Default;
			tsbStop.Enabled = false;
			tsbHome.Enabled = true;
			tsbSearch.Enabled = true;
			tsbRefresh.Enabled = true;
		}

		private void axWebBrowser_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
		{
			HE_WebBrowserTag _HE_WebBrowserTag = (HE_WebBrowserTag)_ActiveWebBrowser.Tag;
			if (_HE_WebBrowserTag._TabIndex != tcBrowsers.SelectedIndex) { return; }

			Cursor.Current = Cursors.Default;
			tsbStop.Enabled = false;
			tsbHome.Enabled = true;
			tsbSearch.Enabled = true;
			tsbRefresh.Enabled = true;
			tsbFind.Enabled = true;
			tspbProgress.Value = 0;
			tsslInfo.Text = "";
		}

		private void axWebBrowser_NewWindow2(object sender, AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
		{
			AxSHDocVw.AxWebBrowser _axWebBrowser = CreateNewWebBrowser();
			e.ppDisp = _axWebBrowser.Application;
			_axWebBrowser.RegisterAsBrowser = true;
		}

		private void axWebBrowser_CommandStateChange(object sender, AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent e)
		{

			AxSHDocVw.AxWebBrowser _AxWebBrowser = (AxSHDocVw.AxWebBrowser)sender;
			HE_WebBrowserTag _HE_WebBrowserTag;
			_HE_WebBrowserTag = (HE_WebBrowserTag)_AxWebBrowser.Tag;

			switch (e.command)
			{
				case ((int)CommandStateChangeConstants.CSC_NAVIGATEFORWARD):
					_HE_WebBrowserTag._CanForward = e.enable;
					break;

				case ((int)CommandStateChangeConstants.CSC_NAVIGATEBACK):
					_HE_WebBrowserTag._CanBack = e.enable;
					break;

				default:
					break;
			}
			_AxWebBrowser.Tag = _HE_WebBrowserTag;

			if (_HE_WebBrowserTag._TabIndex == tcBrowsers.SelectedIndex)
			{
				tsbNext.Enabled = _HE_WebBrowserTag._CanForward;
				tsbBack.Enabled = _HE_WebBrowserTag._CanBack;
			}
		}

		private void axWebBrowser_TitleChange(object sender, AxSHDocVw.DWebBrowserEvents2_TitleChangeEvent e)
		{
			AxSHDocVw.AxWebBrowser _AxWebBrowser = (AxSHDocVw.AxWebBrowser)sender;
			HE_WebBrowserTag _HE_WebBrowserTag;
			_HE_WebBrowserTag = (HE_WebBrowserTag)_AxWebBrowser.Tag;
			tcBrowsers.TabPages[_HE_WebBrowserTag._TabIndex].Text = e.text;
		}

		private void axWebBrowser_WindowClosing(object sender, AxSHDocVw.DWebBrowserEvents2_WindowClosingEvent e)
		{
			e.cancel = true;
			WebBrowserCloseTab();
		}

		#endregion

		/*
		void axWebBrowser_LostFocus(object sender, EventArgs e)
		{
			//Stop music
		}

		void axWebBrowser_GotFocus(object sender, EventArgs e)
		{
			//Start music
		}
		*/

		#region Helper things

		private enum MiscCommandTarget
		{ Find = 1, ViewSource, Options }

		#endregion
	}

	#region Helper things
	//Version 2.00

	//Include the following interface declaration just before your application's namespace
	//declaration to add a reference to the Microsoft HTML (MSHTML) IOleCommandTarget interface
	[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
	public struct OLECMDTEXT
	{
		public uint cmdtextf;
		public uint cwActual;
		public uint cwBuf;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
		public char rgwz;
	}

	[StructLayout(LayoutKind.Sequential)]
	public struct OLECMD
	{
		public uint cmdID;
		public uint cmdf;
	}

	// Interop definition for IOleCommandTarget.
	[ComImport,
	Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),
	InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
	public interface IOleCommandTarget
	{
		//IMPORTANT: The order of the methods is critical here. You
		//perform early binding in most cases, so the order of the methods
		//here MUST match the order of their vtable layout (which is determined
		//by their layout in IDL). The interop calls key off the vtable ordering,
		//not the symbolic names. Therefore, if you switched these method declarations
		//and tried to call the Exec method on an IOleCommandTarget interface from your
		//application, it would translate into a call to the QueryStatus method instead.
		void QueryStatus(ref Guid pguidCmdGroup, UInt32 cCmds,
			[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] OLECMD[] prgCmds, ref OLECMDTEXT CmdText);
		void Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdExecOpt, ref object pvaIn, ref object pvaOut);
	}

	//Each instance of a web browser will have in its tag an HE_WebBrowserTag object
	//that holds some details on that web browser object
	public class HE_WebBrowserTag
	{
		public int _TabIndex = 0;
		public bool _CanBack = false;
		public bool _CanForward = false;
	}

	#endregion
}
